<?php

namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Banners extends BaseModel
{
    protected $table = 'ad';
    protected $tableAdPush = 'ad_push';
    public $timestamps = false;
    public $fillable = ['name','img','url','type','status','outside','auto_time','start_timestamp','end_timestamp','icon'];
    private $fields = ['id','name','img','url','type','status','outside','auto_time','start_timestamp','end_timestamp','wealth_value','gender','channel_no','user_cat', 'sort','action', 'desc','mincharm','maxcharm','minrich','maxrich','minmoney','maxmoney','gift_id','room_ids'];
    private $fieldsAdPush = ['id','ad_id', 'user_ids', 'status', 'push_time', 'created_time', 'operate_time', 'is_del'];

    protected $connection = 'api';

    public function getStartTimestampAttribute($value){
        if(!$value) return 0;
        return date('Y-m-d H:i:s',$value);
    }

    public function getEndTimestampAttribute($value){
        if(!$value) return 0;
        return date('Y-m-d H:i:s',$value);
    }

    //添加
    public function createData($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/ad/create', $data, 'json', 'POST');
    }


    //更新
    public function updateData($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/ad/update', $data, 'json', 'POST');
    }


    //删除
    public function removeData($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/ad/remove', $data, 'urlencoded', 'GET');
    }
    //更新
    public function editPush($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/ad/editPush', $data, 'json', 'POST');
    }

    //删除
    public function delPush($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/ad/delPush', $data, 'json', 'POST');
    }

    /**
     * 获取banner列表
     *
     * @param Request $request
     * @return mixed
     * @author 陈凯强
     */

    public function getList($request){
        $limit = $request->get('limit',10);
        $page = $request->get('page',1);
        $type = $request->get('type' ,0);
        if($type >=0){
            $where['type'] = $type;
        }
        return self::where($where)->orderby('sort','ASC')->paginate($limit, $this->fields,'page',$page);
    }
    /**
     * 搜索banner
     * @param $request
     * @return mixed
     * @author 陈凯强
     */
    public function search($request)
    {
        $limit = $request->get('limit',10);
        $page = $request->get('page',1);
        $kwd = $request->get('kwd' ,'');
        $where = function($q)use($kwd) {
            if (!empty($kwd)) {
                if (is_numeric($kwd))
                {
                    $q->where("id", $kwd);
                }else{
                    $q->where("name", "like",$kwd.'%');
                }
            }
        };
        return self::where($where)->orderby('sort','ASC')->paginate($limit, $this->fields,'page',$page);
    }
    /**
     * 推送日志
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getAdPushList($where=[], $page=1, $limit=10){
        return DB::connection($this->connection)->table($this->tableAdPush)->where($where)->orderBy('id', 'desc')->paginate($limit, $this->fieldsAdPush, 'page', $page);
    }
}
